const connection = require('../app/database')

class CommentService {
  // 插入数据到数据库
  async create(...payload) {

    // 1.获取请求参数
    const [content, moment_id, user_id] = payload

    // 2.定义预处理语句
    const statement = 'INSERT INTO `comment` ( `content`, `moment_id`, user_id) VALUES (?,?,?);'

    // 3.执行sql语句
    let [res, fields] = await connection.execute(statement, [content, moment_id, user_id])
    return res
  }

  async reply(...payload) {

    // 1.获取请求参数
    const [content, moment_id, comment_id, user_id] = payload

    // 2.定义预处理语句
    const statement = 'INSERT INTO `comment` ( `content`, `moment_id`, comment_id, user_id) VALUES (?,?,?,?);'

    // 3.执行sql语句
    let [res, fields] = await connection.execute(statement, [content, moment_id, comment_id, user_id])
    return res
  }

  async remove(...payload) {

    // 1.获取请求参数
    const [ commentId] = payload

    // 2.定义预处理语句
    const statement = 'DELETE FROM comment WHERE id = ?;'

    // 3.执行sql语句
    let [res, fields] = await connection.execute(statement, [ commentId])
    return res
  }
}

module.exports = new CommentService()